from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_session import Session

from models import db
from utils.common import news_style, show_news_status_name, show_news_status_style_name, set_after_request_handle_fuc

from views import index_bp, passport_bp, user_bp, news_bp, admin_bp

app = Flask(__name__)
# 配置信息来自文件
app.config.from_pyfile('config.ini')
# 注册蓝图
app.register_blueprint(index_bp)
app.register_blueprint(passport_bp, url_prefix='/passport')
app.register_blueprint(user_bp, url_prefix='/user')
app.register_blueprint(news_bp, url_prefix='/news')
app.register_blueprint(admin_bp, url_prefix='/admin')
# 模板过滤器
app.add_template_filter(news_style)
app.add_template_filter(show_news_status_name)
app.add_template_filter(show_news_status_style_name)

# 设置钩子函数，可以在调用视图函数之前自动执行
set_after_request_handle_fuc(app)

# 数据库注册到app
db.init_app(app)

# 创建Session对象（此时app这个flask对象中生成的session信息才会存储到Redis里）
Session(app)

manager = Manager(app)

# 生成migrate对象 用来数据库迁移
migrate = Migrate(app, db)
# 添加db命令
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()
